package com.work.count.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.work.count.mapper.IoNumberInfoMapper;
import com.work.count.mapper.WorkTimeMapper;
import com.work.count.pojo.IoNumberInfo;
import com.work.count.pojo.ResultEntity;
import com.work.count.pojo.WorkTime;
import com.work.count.util.JsonUtil;
import com.work.count.util.ReadXlsxUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/workTimeController")
public class WorkTimeController {
    private final WorkTimeMapper mapper;
    private final IoNumberInfoMapper ioMapper;

    public WorkTimeController(WorkTimeMapper mapper, IoNumberInfoMapper ioMapper) {
        this.mapper = mapper;
        this.ioMapper = ioMapper;
    }

    /**
     * @description: 文件导入
     * @param
     * @return: java.lang.String
     * @author: Sue
     * @date: 2021/5/5 0005 0:22
     */
    @PostMapping("/fileUpload")
    public String fileUpload(MultipartFile file){
        FileInputStream inputStream = null;
        try {
            inputStream = (FileInputStream)file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<List> list = ReadXlsxUtil.readXlsx(inputStream);
        List<IoNumberInfo> ioNumberInfosList = list.get(0);
        List<WorkTime> workTimeList = list.get(1);
        workTimeList.forEach(item ->{
            mapper.insert(item);
        });

        ioNumberInfosList.forEach(item ->{
            ioMapper.insert(item);
        });
        return "ok";
    }

    @GetMapping("/search")
    public String search(@RequestParam(defaultValue = "") String userName, 
                         @RequestParam(defaultValue = "") String ioNumber,
                         @RequestParam(defaultValue = "") String deviceName,
                         @RequestParam(defaultValue = "") String startTime,
                         @RequestParam(defaultValue = "")String endTime,
                         @RequestParam(defaultValue = "1") long pageNo,
                         @RequestParam(defaultValue = "10")long pageSize){
        QueryWrapper<WorkTime> wrapper = new QueryWrapper<>();
        Page<WorkTime> workTimePage = new Page<>();
        workTimePage.setSize(pageSize);
        workTimePage.setCurrent(pageNo);
        wrapper.like("user_name",userName)
                .like("io_number",ioNumber)
                .like("device_name",deviceName);
        if (!startTime.isEmpty()) {
            wrapper.between("date_time",startTime,endTime);
        }
        Page<WorkTime> workTimePage1 = mapper.selectPage(workTimePage, wrapper);
        return JsonUtil.getBeanToString(ResultEntity.success(200, "query success", workTimePage1));
    }


    @GetMapping("/searchIoNumber")
    public String searchIoNumber(@RequestParam(value = "ioNumber",defaultValue = "")String ioNumber,
                                 @RequestParam(value = "costCenter",defaultValue = "")String costCenter,
                                 long pageSize,
                                 long pageNo){
        Page<IoNumberInfo> ioNumberInfoPage = new Page<>();
        ioNumberInfoPage.setSize(pageSize);
        ioNumberInfoPage.setCurrent(pageNo);
        QueryWrapper<IoNumberInfo> ioQuery = new QueryWrapper<>();
        ioQuery.like("io_number",ioNumber)
                .like("cost_center",costCenter);
        try {
            Page<IoNumberInfo> ioNumberInfos = ioMapper.selectPage(ioNumberInfoPage, ioQuery);
            return JsonUtil.getBeanToString(ResultEntity.success(200, "query success", ioNumberInfos));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonUtil.getBeanToString(ResultEntity.failed(201, "query failed"));
    }
}
